import json
import random
from typing import Dict, Any, Optional

class CommentGenerator:
    def __init__(self, comment_file: str = 'comment.json'):
        """初始化评价生成器"""
        self.comment_file = comment_file
        self.comment_data: Optional[Dict[str, Any]] = None
        self.load_comment_data()
    
    def load_comment_data(self):
        """加载评价数据"""
        try:
            with open(self.comment_file, 'r', encoding='utf-8') as f:
                self.comment_data = json.load(f)
        except Exception as e:
            print(f"评价数据加载失败: {e}")
            self.comment_data = {}
    
    def generate_comment(self, predicted_level: str, salary_range_text: str) -> Dict[str, str]:
        """根据预测职级生成用户评价"""
        if not self.comment_data or predicted_level not in self.comment_data:
            return {
                "positioning": "暂无市场定位分析",
                "salary": "暂无薪资水平预测",
                "advice": "暂无能力提升建议",
                "growing": "暂无职业发展路径"
            }
        
        level_comments = self.comment_data[predicted_level]
        
        # 从各类评论中随机选择一条
        positioning = random.choice(level_comments.get("positioning", ["暂无市场定位分析"]))
        salary = random.choice(level_comments.get("salary", ["暂无薪资水平预测"]))
        advice = random.choice(level_comments.get("advice", ["暂无能力提升建议"]))
        growing = random.choice(level_comments.get("growing", ["暂无职业发展路径"]))
        
        # 替换薪资范围占位符
        salary = salary.replace("REPLACEMENT_SALARY_RANGE", salary_range_text)
        
        return {
            "positioning": positioning,
            "salary": salary,
            "advice": advice,
            "growing": growing
        }